package blackmamba.threads.runnable_enemy;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ThredsTestPanel extends JPanel {
	
	private int delay = 10;
	private int minModulo = 1;
	
	private int startValue;
	private int width;
	private int maxModulo;
	
	public ThredsTestPanel( int n) {
		super();
		setLayout(new BorderLayout());
		
		width = 800/n;
		if(width > 100 ){
			width = 100;
		}
		maxModulo = width;
		startValue = maxModulo/2;
		
		Thread thread;
		JPanel threadsPanel = null;
		RunnableEnemyTest enemy;
		ArrayList<RunnableEnemyTest> enemies = new ArrayList<RunnableEnemyTest>();

		
		JPanel slidersPanel = new  JPanel();
		slidersPanel.setLayout(new BoxLayout(slidersPanel, BoxLayout.Y_AXIS));
		
		JButton b1 = new JButton("Theta uguale");
		b1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				for (RunnableEnemyTest tmpEnemy : enemies) {
					tmpEnemy.setTheta( 0 );
				}
			}
		});
		slidersPanel.add(b1);
		
		JButton b2 = new JButton("Theta casuale");
		b2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Random r = new Random();
				int randomTheta;
				for (RunnableEnemyTest tmpEnemy : enemies) {
					randomTheta =  r.nextInt(180);
					tmpEnemy.setTheta( randomTheta );
				}
			}
		});
		slidersPanel.add(b2);
		
		
		
		TestSlider phiSlider = new TestSlider(0, 180, 0);
		phiSlider.setSliderChangeListener("Fase", new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider slider = (JSlider)e.getSource();
				for (RunnableEnemyTest tmpEnemy : enemies) {
					tmpEnemy.setPhi( slider.getValue() );
				}
			}
			
		});
		slidersPanel.add(phiSlider);
		
		TestSlider sliderModulo = new TestSlider(minModulo, maxModulo, startValue );
		sliderModulo.setSliderChangeListener("Modulo", new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider slider = (JSlider)e.getSource();
				for (RunnableEnemyTest tmpEnemy : enemies) {
					tmpEnemy.setModulo( slider.getValue() );
				}
			}
			
		});
		slidersPanel.add(sliderModulo); 
		
		
		TestSlider speedSlider = new TestSlider(1, 15, 5);
		speedSlider.setSliderChangeListener("Speed", new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider slider = (JSlider)e.getSource();
				for (RunnableEnemyTest tmpEnemy : enemies) {
					tmpEnemy.setSpeed( slider.getValue() );
				}
			}
		});
		slidersPanel.add(speedSlider);
		
		
		TestSlider sliderDelay = new TestSlider(1, 1000, delay);
		sliderDelay.setSliderChangeListener("Delay in millisecondi", new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider slider = (JSlider)e.getSource();
				for (RunnableEnemyTest tmpEnemy : enemies) {
					tmpEnemy.setDelay( slider.getValue() );
				}
			}
		});
		slidersPanel.add(sliderDelay);
		
		add(slidersPanel, BorderLayout.WEST);
		
		createThredsPanels(n, delay, width, startValue, enemies, threadsPanel);
		
	}

	
	private void createThredsPanels(int n, long delay, int width,
			int startValue, ArrayList<RunnableEnemyTest> enemies,
			JPanel threadsPanel) {
		
		threadsPanel = new JPanel();
		
		FlowLayout layout = new FlowLayout();
		layout.setHgap(10);
		layout.setVgap(10);
		threadsPanel.setLayout(layout);
		add(threadsPanel, BorderLayout.CENTER);
		Thread thread;
		RunnableEnemyTest enemy;
		for (int i = 0; i < n; i++) {
			
			enemy = new RunnableEnemyTest(i+"", width, startValue );
			thread = new Thread( enemy );
			enemy.setDelay(delay);
			threadsPanel.add(enemy);
			thread.start();
			enemies.add(enemy);
			
		}
	}

}
